a tool for shared writing and social publishing
at feature/post-options 69 lines 2.0 kB view raw
1import { z } from "zod"; 2import { makeRoute } from "../lib"; 3import type { Env } from "./route"; 4import { idResolver } from "app/(home-pages)/reader/idResolver"; 5import { supabaseServerClient } from "supabase/serverClient"; 6import { Agent } from "@atproto/api"; 7import { getIdentityData } from "actions/getIdentityData"; 8import { createOauthClient } from "src/atproto-oauth"; 9 10export type GetProfileDataReturnType = Awaited< 11 ReturnType<(typeof get_profile_data)["handler"]> 12>; 13 14export const get_profile_data = makeRoute({ 15 route: "get_profile_data", 16 input: z.object({ 17 didOrHandle: z.string(), 18 }), 19 handler: async ({ didOrHandle }, { supabase }: Pick<Env, "supabase">) => { 20 // Resolve handle to DID if necessary 21 let did = didOrHandle; 22 23 if (!didOrHandle.startsWith("did:")) { 24 const resolved = await idResolver.handle.resolve(didOrHandle); 25 if (!resolved) { 26 throw new Error("Could not resolve handle to DID"); 27 } 28 did = resolved; 29 } 30 let agent; 31 let authed_identity = await getIdentityData(); 32 if (authed_identity?.atp_did) { 33 try { 34 const oauthClient = await createOauthClient(); 35 let credentialSession = await oauthClient.restore( 36 authed_identity.atp_did, 37 ); 38 agent = new Agent(credentialSession); 39 } catch (e) { 40 agent = new Agent({ 41 service: "https://public.api.bsky.app", 42 }); 43 } 44 } else { 45 agent = new Agent({ 46 service: "https://public.api.bsky.app", 47 }); 48 } 49 50 let profileReq = agent.app.bsky.actor.getProfile({ actor: did }); 51 52 let publicationsReq = supabase 53 .from("publications") 54 .select("*") 55 .eq("identity_did", did); 56 57 let [{ data: profile }, { data: publications }] = await Promise.all([ 58 profileReq, 59 publicationsReq, 60 ]); 61 62 return { 63 result: { 64 profile, 65 publications: publications || [], 66 }, 67 }; 68 }, 69});